﻿Imports BUS
Imports DTO
Public Class frmGhiNhanMatSach : Inherits DevComponents.DotNetBar.Office2007Form
	 Private m_Nhanvien As CNhanVienDTO
    Public Property NhanVien() As CNhanVienDTO
        Get
            Return m_Nhanvien
        End Get
        Set(ByVal value As CNhanVienDTO)
            m_Nhanvien = value
        End Set
    End Property
	
    Private Sub Reload()
        Dim stt As Integer = 0
        dgvChiTietDanhSachPhieuMuon.Rows.Clear()
        Dim lphieumuon As List(Of CChiTietPhieuMuonDTO) = New CChiTiepPhieuMuonBUS().LayChiTietPhieuMuonTheoMa(txtMaDocGia.Text)
        For Each ctpm As CChiTietPhieuMuonDTO In lphieumuon
            stt += 1
            dgvChiTietDanhSachPhieuMuon.Rows.Add(stt, ctpm.MaPhieuMuon, ctpm.Ma, ctpm.MaSach, New CSachBUS().LayTenSach(ctpm.MaSach), New CSachBUS().LayTienPhatKhiBietMaSach(ctpm.MaSach), False)
        Next
    End Sub
    Private Sub btnTim_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTim.Click
        If txtMaDocGia.Text = "" Then
            MessageBox.Show("Mã độc giả không hợp lệ")
        End If

        Dim dg As CDocGiaDTO = New CDocGiaBUS().LayDocgiaKhiBietMaCoTheHetHan(Convert.ToInt32(txtMaDocGia.Text))
        If dg Is Nothing Then
            MessageBox.Show("Mã độc giả không đúng")
        End If

        txtTenDocGia.Text = dg.HoTen
        Dim stt As Integer = 0
        Reload()
    End Sub

    Private Sub dgvChiTietDanhSachPhieuMuon_CurrentCellDirtyStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvChiTietDanhSachPhieuMuon.CurrentCellDirtyStateChanged
        If TypeOf dgvChiTietDanhSachPhieuMuon.CurrentCell Is DataGridViewCheckBoxCell Then
            dgvChiTietDanhSachPhieuMuon.CommitEdit(DataGridViewDataErrorContexts.Commit)
        End If
    End Sub

    Private Sub dgvChiTietDanhSachPhieuMuon_CellValueChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvChiTietDanhSachPhieuMuon.CellValueChanged
        If e.RowIndex = -1 Then
            Return
        End If
        Dim tien As Integer = 0
        For Each dgr As DataGridViewRow In dgvChiTietDanhSachPhieuMuon.Rows
            If dgr.Cells("clMat").Value Then
                tien += Convert.ToInt32(dgr.Cells("clTienPhat").Value)
            End If
        Next
        txtTienPhat.Text = tien.ToString()
    End Sub

    Private Sub Reset()
        txtMaDocGia.Text = ""
        txtTenDocGia.Text = ""
        txtTienPhat.Text = ""
        dtpNgay.Value = DateTime.Now
    End Sub
    Private Sub btnHuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Reset()
    End Sub

    Private Sub btnInKetQua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInKetQua.Click
        Dim ghinhan As CGhiNhanMatSachDTO
        For Each dgr As DataGridViewRow In dgvChiTietDanhSachPhieuMuon.Rows
            If dgr.Cells("clMat").Value Then
                ghinhan = New CGhiNhanMatSachDTO()
                ghinhan.MaDocGia = txtMaDocGia.Text
                ghinhan.MaNhanVien = txtMaNhanVienGhiNhan.Text
                ghinhan.MaSach = dgr.Cells("clMaSach").Value
                ghinhan.NgayGhiNhan = dtpNgay.Value
                ghinhan.MaChiTietPhieu = dgr.Cells("clMaChiTiet").Value
                Dim check As Boolean = New CGhiNhanMatSachBUS().ThemGhiNhan(ghinhan)
            End If
        Next
        MessageBox.Show("Ghi nhận thành công")
        Reload()
    End Sub


    Private Sub frmTimPhieuMuonTheoMaDocGia_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dtpNgay.Value = DateTime.Now
		txtMaNhanVienGhiNhan.Text=m_Nhanvien.Ma
		txtTenNhanVienGhiNhan.Text=m_Nhanvien.HoTen
    End Sub
End Class